package com.zhu.storesmanager;

import java.util.ArrayList;
import java.util.List;

import com.zhu.storesmanager.models.BrandsDataItem;
import com.zhu.storesmanager.models.DataItem;
import com.zhu.storesmanager.models.ProjectDataItem;
import com.zhu.storesmanager.models.SyncModel;
import com.zhu.storesmanager.models.VisitTaskInfo;
import com.zhu.storesmanager.provider.StoresManagerSettings;
import com.zhu.storesmanager.utils.Actions;
import com.zhu.storesmanager.utils.DataUtils;
import com.zhu.storesmanager.utils.DatabaseUtil;
import com.zhu.storesmanager.utils.ViewsUtil;
import com.zhu.storesmanager.views.BrandInfoView;
import com.zhu.storesmanager.views.ChoiceView;
import com.zhu.storesmanager.views.DateView;
import com.zhu.storesmanager.views.PopBrandInfoTextView;
import com.zhu.storesmanager.views.PopMenu;
import com.zhu.storesmanager.views.PopTextView;
import com.zhu.storesmanager.views.SampleModelsWallView;

import android.app.Activity;
import android.app.Dialog;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * 门店销量界面
 */
public class SvStoreSalesActivity extends Activity implements OnClickListener{

	//异步查询数据库token
	private final static int DATAITEM_QUERY_TOKEN = 0; //查询所有数据词典
	private final static int SALES_DATA_QUERY_TOKEN = 1; //查询SALES数据
	private final static int PROJECT_DATA_QUERY_TOKEN = 2; //拜访文本数据
	private DataQueryHandler mDataQueryHandler = null; //数据异步查询Handler

    private long mDBid = -1; //数据库保存数据ID
    private VisitTaskInfo mVisitTaskInfo = null; //拜访门店数据
    private DateView mDateView = null; //上月日期
    private EditText mAllsalesEditText = null; //总销量
    private EditText mAllmoneyEditText = null; //总金额
    private EditText mSellcountEditText = null; //销售台数
    private EditText mSellmoneyEditText = null; //销售金额

    private ChoiceView mBrandsChoiceView = null; //品牌选择
    private BrandInfoView mBrandInfoView = null; //品牌信息
    private RadioGroup mCollectRadioGroup = null;//收集
    private RadioGroup mCheckRadioGroup = null;  //核对
    private RadioGroup mStockRadioGroup = null;  //库存
    private SampleModelsWallView mSampleModelsWallView = null; //希望补充货源
    private PopBrandInfoTextView mPopTextView = null; //品牌详细信息
    private PopMenu mPopMenu = null; //弹出菜单
    private boolean isDataChange = true; //数据是否发生
    private List<ProjectDataItem> mProjectList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parcelable parcelable = getIntent().getParcelableExtra("info");
        mVisitTaskInfo = (VisitTaskInfo) parcelable;
        setContentView(R.layout.activity_sv_storessales);

        setupVies();
        initData();
    }

    /**
     * 初始化界面
     */
    private void setupVies(){
    	mPopTextView = new PopBrandInfoTextView((FrameLayout)findViewById(R.id.layout));
        ((TextView) findViewById(R.id.title_text)).setText(R.string.store_sales);
        findViewById(R.id.svbottom_menu_back).setOnClickListener(this);
        findViewById(R.id.svbottom_menu_save).setOnClickListener(this);
        findViewById(R.id.svbottom_menu_menu).setOnClickListener(this);
        findViewById(R.id.svstoresales_newadd).setOnClickListener(this);
        mDateView = (DateView)findViewById(R.id.svstoresales_lastdate);
        mDateView.initData(this, DateView.TYPE_MONTH, -1);
        mDateView.setText(Actions.getLaseDate(this));
        mAllsalesEditText = setEditTextView(R.id.svstoresales_allsales, R.string.sales_allsales);
        mAllmoneyEditText = setEditTextView(R.id.svstoresales_allmoney, R.string.sales_allmoney);
        mSellcountEditText = setEditTextView(R.id.svstoresales_sellcount, R.string.sales_sellcount);
        mSellmoneyEditText = setEditTextView(R.id.svstoresales_sellmoney, R.string.sales_sellmoney);
        mBrandsChoiceView = setChoiceView(R.id.svstoresales_brands, R.string.sales_brands);

        mBrandInfoView = (BrandInfoView)findViewById(R.id.svstoresales_bandlist);
        mBrandInfoView.setPopTextView(mPopTextView);
        mCollectRadioGroup = setRadioGroupView(R.id.svstoresales_collect, 
                R.string.sales_collect, R.string.dialog_yes, R.string.dialog_no);
        mCheckRadioGroup = setRadioGroupView(R.id.svstoresales_check,
                R.string.sales_check, R.string.dialog_yes, R.string.dialog_no);
        mStockRadioGroup = setRadioGroupView(R.id.svstoresales_stock,
                R.string.sales_check, R.string.stock_enough, R.string.stock_shortage);
        mSampleModelsWallView = (SampleModelsWallView)findViewById(R.id.svstoresales_addres);
        mSampleModelsWallView.setupViews(this, SampleModelsWallView.TYPE_NEEDADDRES);
        mPopMenu = new PopMenu(this, findViewById(R.id.bottom_menu), PopMenu.MENU_SALES);
        mPopMenu.setVisitTaskInfo(mVisitTaskInfo);
    }

    /**
     * 设置输入文本界面
     * @return editText 输入框
     * @param parentId 父ID
     * @param textId 文本id
     */
    private EditText setEditTextView(int parentId, int textId){
        return ViewsUtil.setEditTextView(findViewById(parentId), textId);
    }

    /**
     * 设置选项组界面
     * @return editText 输入框
     * @param parentId 父ID
     * @param textId 文本id
     * @param childtext1Id 按钮1文本
     * @param childtext2Id 按钮2文本
     */
    private RadioGroup setRadioGroupView(int parentId, int textId, int childtext1Id, int childtext2Id){
        View view = findViewById(parentId);
        RadioGroup radioGroup = (RadioGroup)view.findViewById(R.id.radiogroup);
        ((TextView)view.findViewById(R.id.radiogroup_label)).setText(textId);
        ((RadioButton)radioGroup.findViewById(R.id.radiobutton1)).setText(childtext1Id);
        ((RadioButton)radioGroup.findViewById(R.id.radiobutton2)).setText(childtext2Id);
        return radioGroup;
    }

    /**
     * 设置选项组界面标签文本
     * @param parentId 父ID
     * @param text 文本内容
     */
    private void setRadioGroupLableText(int parentId, String text){
        View view = findViewById(parentId);
        ((TextView)view.findViewById(R.id.radiogroup_label)).setText(text);
    }

    /**
     * 设置选择框界面
     * @return ChoiceView 选择框
     * @param parentId 父ID
     * @param textId 文本id
     */
    private ChoiceView setChoiceView(int parentId, int textId){
        return ViewsUtil.setChoiceView(findViewById(parentId), textId);
    }

    /**
	 * 初始化话数据
	 */
	private void initData(){
		if(!DataUtils.mUserData.isAdmin()){ //检测非管理员
			if(mDataQueryHandler == null){
				mDataQueryHandler = new DataQueryHandler(getContentResolver());	
			}
			startQueryProjects();
		}else{
    		setDataFromAdminSearchData();
    	}
	}

	/**
	 * 开始查询数据词典数据
	 */
	private void startQueryDataItemList(){
		//查询指定类型的型号列表数据
		String where = StoresManagerSettings.DataDictionaryColumns.DATA_TYPE + "=" + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_BRANDS;
		mDataQueryHandler.startQuery(DATAITEM_QUERY_TOKEN, null, 
				StoresManagerSettings.DataDictionaryColumns.CONTENT_URI_NO_NOTIFICATION,
				null, where, null, null);
	}

	/**
     * 开始查询销量数据
     */
    private void startQuerySalesData(){
        String where = StoresManagerSettings.SupervisorStoreSalesColumns.STOREID + "=\'" + mVisitTaskInfo.mStoreId+ "\'";
        mDataQueryHandler.startQuery(SALES_DATA_QUERY_TOKEN, null, 
                StoresManagerSettings.SupervisorStoreSalesColumns.CONTENT_URI_NO_NOTIFICATION,
                null, where, null, null);
    }

    /**
     * 查询project数据
     */
    private void startQueryProjects(){
    	String where = StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE + "=" + StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES;
    	mDataQueryHandler.startQuery(PROJECT_DATA_QUERY_TOKEN, null, 
				StoresManagerSettings.DataProjectsColumns.CONTENT_URI_NO_NOTIFICATION,
				null, where, null, null);
    }

    /**
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v != null){
            switch(v.getId()){
                case R.id.svbottom_menu_back : //返回
                	if(!doBack()){
                        finish();
                    }
                    break;
                case R.id.svbottom_menu_save: //保存
                    saveData();
                    break;
                case R.id.svstoresales_newadd://添加品牌销量数据
                	addBrandSalesData();
                	break;
                case R.id.svbottom_menu_menu: //菜单
                	mPopMenu.showMenu();
                	break;
            }
        }
    }

    /**
     * @param requestCode 请求代码
     * @param resultCode 返回状态
     * @param data 返回数据
     * (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode != Activity.RESULT_OK){
            return;
        }
        switch(requestCode){
            case SampleModelsWallView.TYPE_NEEDADDRES:
            	mSampleModelsWallView.addModel(data.getStringExtra(SvInquiryModelResultActivity.DATA));
                break;
        }
    }

    /**
	 * 异步数据查询
	 */
	private final class DataQueryHandler extends AsyncQueryHandler {
        public DataQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }

        /**
         * 查询结果返回
         * (non-Javadoc)
         * @see android.content.AsyncQueryHandler#onQueryComplete(int, java.lang.Object, android.database.Cursor)
         */
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            switch (token) {
                case DATAITEM_QUERY_TOKEN:
                	updateDataItemFromCursor(cursor);
                    break;
                case SALES_DATA_QUERY_TOKEN:
                    updateDataFromCursor(cursor);
                    break;
                case PROJECT_DATA_QUERY_TOKEN:
                	updateProjectDataFromCursor(cursor);
                    break;
                default:
                    Log.e("zhu", "onQueryComplete called with unknown token " + token);
            }
        }

        @Override
        protected void onDeleteComplete(int token, Object cookie, int result) {

        }
    }

	/**
	 * 更新选择栏数据词典数据
	 */
	private void updateDataItemFromCursor(Cursor cursor){
		try {
			List<DataItem> list = new ArrayList<DataItem>(); //品牌数据集

            final int dataIDIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.DataDictionaryColumns._ID);
            final int dataNameIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.DataDictionaryColumns.DATA_NAME);
            final int dataTypeIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.DataDictionaryColumns.DATA_TYPE);
            while (cursor.moveToNext()) {//遍历所有型号数据
            	DataItem item = new DataItem();
            	item.mDataId = cursor.getString(dataIDIndex);
            	item.mDataName = cursor.getString(dataNameIndex);
            	item.mDataType = cursor.getInt(dataTypeIndex);
            	list.add(item);
            }
            //更新界面数据
            mBrandsChoiceView.setChoiceDataList(list);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zhu", "updateDataItemFromCursor error");
        } finally {
        	if(cursor != null){
        		cursor.close();
        		cursor = null;
        	}
        }
        startQuerySalesData();
	}

	/**
     * 更新数据从数据库保存的数据
     * @param cursor 数据
     */
    private void updateDataFromCursor(Cursor cursor){
        if(cursor != null && cursor.moveToFirst() && cursor.getCount() > 0){
        	boolean isSaved = cursor.getInt(cursor.getColumnIndexOrThrow(
    				StoresManagerSettings.SAVED)) == 1 ? true : false;
        	int dateIndex = cursor.getColumnIndexOrThrow(
        			StoresManagerSettings.SupervisorStoreSalesColumns.SALES_DATE);
            int iAllsales = cursor.getInt(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ALLCOUNT));
            int iAllmoney = cursor.getInt(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ALLMONEY));
            String brands = cursor.getString(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSalesColumns.SALES_BRANDS));
            int iCollect = cursor.getInt(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSalesColumns.SALES_COLLECT));
            int iCheck = cursor.getInt(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSalesColumns.SALES_CHECK));
            int iStock = cursor.getInt(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSalesColumns.SALES_STOCK));
            String models = cursor.getString(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ADDMODELS));

            mDBid = cursor.getLong(cursor.getColumnIndexOrThrow(
                    StoresManagerSettings.SupervisorStoreSampleColumns._ID));
            brands = DataUtils.updateStringDataFromHistoryData(mProjectList, brands, isSaved, 
    				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 1);
            mSampleModelsWallView.addModel(DataUtils.updateStringDataFromHistoryData(mProjectList, models, isSaved, 
    				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 5));
            mSampleModelsWallView.setSelectedStatus(cursor.getInt(cursor.getColumnIndexOrThrow(
    				StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ADDSELECTED)) == 1 ? true : false);
            mAllsalesEditText.setText(DataUtils.updateStringDataFromHistoryData(mProjectList, iAllsales +"", isSaved, 
    				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 1));
            mAllmoneyEditText.setText(DataUtils.updateStringDataFromHistoryData(mProjectList, iAllmoney +"", isSaved, 
    				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 1));

            iCollect = DataUtils.updateIntDataFromHistoryData(mProjectList, iCollect, isSaved, 
    				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 2);
            if(iCollect != -1){
                mCollectRadioGroup.check(mCollectRadioGroup.getChildAt(iCollect).getId());    
            }
            iCheck = DataUtils.updateIntDataFromHistoryData(mProjectList, iCheck, isSaved, 
    				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 3);
            if(iCheck != -1){
                mCheckRadioGroup.check(mCheckRadioGroup.getChildAt(iCheck).getId());    
            }
            iStock = DataUtils.updateIntDataFromHistoryData(mProjectList, iStock, isSaved, 
    				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 4);
            if(iStock != -1){
                mStockRadioGroup.check(mStockRadioGroup.getChildAt(iStock).getId());    
            }
            mBrandInfoView.setBrandData(brands);
            if(!isSaved){
            	mDateView.setSavedData(cursor.getLong(dateIndex));
            }
        }
        if(cursor != null){
            cursor.close();
            cursor = null;
        }
    }

	/**
	 * 添加品牌销量数据
	 */
	private void addBrandSalesData(){
        int iAllsales = getEditTextContent(mAllsalesEditText);
        int iAllmoney = getEditTextContent(mAllmoneyEditText);
        int iSellcount = getEditTextContent(mSellcountEditText);
        int iSellmoney = getEditTextContent(mSellmoneyEditText);
        if(iAllsales == 0 || iAllmoney == 0 || iSellcount == 0 || iSellcount == 0){ //总销量或者总金额or销售台数 or销售金额不能为空
        	Actions.toastShort(this, R.string.sales_all_notnull);
        	return;
        }
        if(iSellcount > iAllsales || iSellmoney > iAllmoney){ //单个销量不能大于总销量
        	Actions.toastShort(this, R.string.sales_dataerror);
        	return;
        }
        DataItem dateItem = mBrandsChoiceView.getSingleSelectedDataItem(); //选中的品牌
        if(dateItem == null){
        	Actions.toastShort(this, R.string.sales_not_selectbrand);
        	return;
        }
        //构建品牌数据项
        BrandsDataItem brandsDataItem = new BrandsDataItem();
        brandsDataItem.mBrandId = dateItem.mDataId;
        brandsDataItem.mBrandName = dateItem.mDataName;
        brandsDataItem.mAllSellCount = iAllsales;
        brandsDataItem.mAllSellMoney = iAllmoney;
        brandsDataItem.mSellCount = iSellcount;
        brandsDataItem.mSellMoney = iSellmoney;
        mBrandInfoView.addBrandsData(brandsDataItem);
	}

	/**
	 * 获取输入框输入的数据
	 * @param editText 输入框
	 * @return data 输入的数值
	 */
	private int getEditTextContent(EditText editText){
		String text = editText.getText().toString().trim();
		int data = 0;
		if(text != null && text.length() > 0){
			try{
				data = Integer.parseInt(text);
			}catch(Exception e){
				Log.e("zhu", "text="+text);
			}
		}
		return data;
	}

	/**
     * 按键事件处理
     */
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if(!doBack()){
                finish();
            }
            return true;
        }
        return false;
    }

    /**
     * 返回事件处理
     */
    private boolean doBack(){
        if(mPopTextView != null && mPopTextView.doBack()){
            return true;
        }
        if(isDataChange){
        	if(DataUtils.mUserData.isAdmin()){
        		return false;
        	}
            popExitDialog();
            return true;
        }
        return false;
    }

    /**
     * 保存数据
     */
    private void saveData(){
        int iAllsales = getEditTextContent(mAllsalesEditText);
        int iAllmoney = getEditTextContent(mAllmoneyEditText);
        String brands = mBrandInfoView.getSavedData();
        int iCollect = getCheckedRadioButtonIndex(mCollectRadioGroup);
        int iCheck = getCheckedRadioButtonIndex(mCheckRadioGroup);
        int iStock = getCheckedRadioButtonIndex(mStockRadioGroup);
        String models = mSampleModelsWallView.getModels();
        ContentValues values = new ContentValues();
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_DATE, mDateView.getTime());
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ALLCOUNT, iAllsales);
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ALLMONEY, iAllmoney);
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_BRANDS, brands);
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_COLLECT, iCollect);
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_CHECK, iCheck);
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_STOCK, iStock);
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ADDSELECTED, mSampleModelsWallView.isSelected() ? 1 : 0);
        values.put(StoresManagerSettings.SupervisorStoreSalesColumns.SALES_ADDMODELS, models);
        values.put(StoresManagerSettings.SAVED, 0);
        if(checkSalesData(iAllsales, iAllmoney, brands)
        		&& Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 2), iCollect)
        		&& Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 3), iCheck)
        		&& Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 4), iStock)
        		&& mSampleModelsWallView.checkDataCompelete(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 5))){
        	DatabaseUtil.saveVisitStatus(this, DatabaseUtil.TYPE_SALES , Integer.parseInt(mVisitTaskInfo.mVisitId), 1);
        }else{
        	DatabaseUtil.saveVisitStatus(this, DatabaseUtil.TYPE_SALES , Integer.parseInt(mVisitTaskInfo.mVisitId), 0);
        }
        if(mDBid == -1){ //如果不存在数据 则插入新数据
            values.put(StoresManagerSettings.SupervisorStoreSalesColumns.STOREID, mVisitTaskInfo.mStoreId);
            Uri uri = getContentResolver().insert(StoresManagerSettings.SupervisorStoreSalesColumns.CONTENT_URI_NO_NOTIFICATION, values);
            mDBid = Long.parseLong(uri.getPathSegments().get(1));
        }else{ //存在则更新历史数据
            getContentResolver().update(StoresManagerSettings.SupervisorStoreSalesColumns.getContentUri(mDBid, false), values, null, null);
        }
        Actions.toastShort(this, this.getString(R.string.datasave_success));
    }

    /**
     * 检测销量数据是否填写完整
     * @param iAllsales 销量
     * @param iAllmoney 销售金额
     * @param brands 销售信息
     * @return true 数据填写完成 否则false
     */
    private boolean checkSalesData(int iAllsales, int iAllmoney, String brands){
    	if(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES, 1)){
    		if(iAllsales > 0 && iAllmoney > 0 && !Actions.isNull(brands)){
    			return true;
    		}else{
    			return false;
    		}
    	}
    	return true;
    }

    /**
     *获取选中项的索引 
     *@param radioGroup 选项组
     *@param index 选中索引 -1为未选中
     */
    private int getCheckedRadioButtonIndex(RadioGroup radioGroup){
        int index = radioGroup.getCheckedRadioButtonId();
        if(index != -1){
            index = radioGroup.indexOfChild(radioGroup.findViewById(index));
        }
        return index;
    }

    /**
	 * 更新project数据从数据库
	 * @param cursor 数据库数据
	 */
	private void updateProjectDataFromCursor(Cursor cursor){
		mProjectList = DatabaseUtil.getProjectDataList(cursor);
		if(mProjectList != null && mProjectList.size() > 0){
			for(ProjectDataItem item : mProjectList){
				updateViewFromProject(item);
			}
		}
		startQueryDataItemList();
	}

    /**
	 * 从project数据更新界面
	 */
	private void updateViewFromProject(ProjectDataItem item) {
		if (item.mId == 1) {
			if (item.isShow) {
				((TextView) findViewById(R.id.sales_info_labletext)).setText(item.mName);
			} else {
				findViewById(R.id.sales_info_labletext).setVisibility(View.GONE);
			}
		} else if (item.mId == 2) {
			if (item.isShow) {
				setRadioGroupLableText(R.id.svstoresales_collect, item.mName);
			} else {
				findViewById(R.id.svstoresales_collect).setVisibility(View.GONE);
			}
		} else if (item.mId == 3) {
			if (item.isShow) {
				setRadioGroupLableText(R.id.svstoresales_check, item.mName);
			} else {
				findViewById(R.id.svstoresales_check).setVisibility(View.GONE);
			}
		} else if (item.mId == 4) {
			if (item.isShow) {
				setRadioGroupLableText(R.id.svstoresales_stock, item.mName);
			} else {
				findViewById(R.id.svstoresales_stock).setVisibility(View.GONE);
			}
		} else if (item.mId == 5) {
			if (item.isShow) {
				mSampleModelsWallView.setLabelText(item.mName);
			} else {
				mSampleModelsWallView.setVisibility(View.GONE);
			}
		}
	}

	/**
	 * 创建日期选择dialog
	 * @param id 创建dialogID
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	protected Dialog onCreateDialog(int id) {
		if(mDateView != null){
			return mDateView.onCreateDialog(id);
		}
		return null;
	}

	/**
	 * 准备dialog数据
	 * (non-Javadoc)
	 * @see android.app.Activity#onPrepareDialog(int, android.app.Dialog)
	 */
	 protected void onPrepareDialog(int id, Dialog dialog) { 
		 if(mDateView != null){
			mDateView.onPrepareDialog(id, dialog);
		 }
	 }

	/**
	 * 从管理员查询结果数据更新界面
	 */
	private void setDataFromAdminSearchData() {
		findViewById(R.id.svbottom_split_menu).setVisibility(View.GONE);
		findViewById(R.id.svbottom_menu_save).setVisibility(View.GONE);
		if (DataUtils.mQueryVisitContentResult.Data != null && DataUtils.mDesDataVisitContentSales != null) {
			mSampleModelsWallView.addModel(DataUtils.mQueryVisitContentResult.Data.OutOfStockModels);
			mSampleModelsWallView.setEnabled(false);
			mAllsalesEditText.setText(DataUtils.mDesDataVisitContentSales.SaleNum + "");
			mAllsalesEditText.setEnabled(false);
			mAllmoneyEditText.setText(DataUtils.mDesDataVisitContentSales.SaleAmount + "");
			mAllmoneyEditText.setEnabled(false);
			int iCollect = getShiFouIndex(DataUtils.mQueryVisitContentResult.Data.LedgerGatherStatus);
			if (iCollect != -1) {
				mCollectRadioGroup.check(mCollectRadioGroup.getChildAt(iCollect).getId());
			}
			ViewsUtil.setRadioGroupdisable(mCollectRadioGroup);
			int iCheck = getShiFouIndex(DataUtils.mQueryVisitContentResult.Data.LedgerCheckStatus);
			if (iCheck != -1) {
				mCheckRadioGroup.check(mCheckRadioGroup.getChildAt(iCheck).getId());
			}
			ViewsUtil.setRadioGroupdisable(mCheckRadioGroup);
			int iStock = getQueFanIndex(DataUtils.mQueryVisitContentResult.Data.CounterStockStatus);
			if (iStock != -1) {
				mStockRadioGroup.check(mStockRadioGroup.getChildAt(iStock).getId());
			}
			ViewsUtil.setRadioGroupdisable(mStockRadioGroup);
			if (DataUtils.mDesDataVisitContentSales.SaleMonth != null) {
				mDateView.setText(DataUtils.mDesDataVisitContentSales.SaleMonth);
			}
			mDateView.setEnabled(false);

			findViewById(R.id.svstoresales_sellcount).setVisibility(View.GONE);
			findViewById(R.id.svstoresales_brands).setVisibility(View.GONE);
			findViewById(R.id.svstoresales_sellmoney).setVisibility(View.GONE);
			findViewById(R.id.svstoresales_newadd).setVisibility(View.GONE);
			if(DataUtils.mDesDataVisitContentSales.Sales != null){
				for(SyncModel.DataVisitContentSales sale : DataUtils.mDesDataVisitContentSales.Sales){ //添加销量数据
					BrandsDataItem item = new BrandsDataItem();
					item.mAllSellCount = DataUtils.mDesDataVisitContentSales.SaleNum;
					item.mAllSellMoney = DataUtils.mDesDataVisitContentSales.SaleAmount;
					item.mBrandName = sale.Brands;
					item.mSellCount = sale.BrandsSaleNum;
					item.mSellMoney = sale.BrandsSaleAmount;
					mBrandInfoView.addBrandsData(item);
				}
			}
			
			List<ProjectDataItem> list = DataUtils
					.getProjectDataList(StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_SALES);
			if (list != null && list.size() > 0) {
				for (ProjectDataItem item : list) {
					updateViewFromProject(item);
				}
			}
		}
	}

	/**
	 * 获取是否index
	 */
	private int getShiFouIndex(String status){
		if(status != null){
			if(getString(R.string.dialog_yes).equals(status)){
				return 0;
			}else if(getString(R.string.dialog_no).equals(status)){
				return 1;
			}	
		}
		return -1;
	}

	/**
	 * 获取缺乏index
	 */
	private int getQueFanIndex(String status){
		if(status != null){
			if(getString(R.string.stock_enough).equals(status)){
				return 0;
			}else if(getString(R.string.stock_shortage).equals(status)){
				return 1;
			}	
		}
		return -1;
	}

	/**
     * 退出提示
     * 
     */
    private void popExitDialog() {
    	Actions.popExitDialog(this);
    }
}
